package com.jian.java;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Jiange
 * @create 2021-04-05-14:08
 */
public class Dom4jTest {
    @Test
    public void test1() throws Exception {
        //创建一个SAXreader输入流，读取xml配置文件，生成Document对象
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read("xml/books.xml");
        //获取根元素
        Element rootElement = read.getRootElement();
        //通过根元素获取子元素 element  elements
        List<Element> books = rootElement.elements("book");
        //asXML将xml对象转变成字符串
        books.forEach(e -> {
            Element element = e.element("name");
            String text = element.getText();
            String price = e.elementTextTrim("price");//直接获取指定标签的文本内容
            System.out.println(text);
            System.out.println(price);
            String sno = e.attributeValue("sno");
            System.out.println(sno);
            System.out.println(Double.parseDouble(price)+"--------");
            System.out.println(new Book(sno,text,e.elementText("author"),BigDecimal.valueOf(Double.parseDouble(price))));
        });
    }
}
